//
//  SDWeatherDTO.m
//  SuperDoctor
//
//  Created by 李忠 on 7/6/16.
//  Copyright © 2016 SD. All rights reserved.
//

#import "SDWeatherModel.h"
#import "SDNetWorkTool.h"

@implementation SDWeatherModel

+ (instancetype)shareManager{
    
    static SDWeatherModel *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        instance = [[SDWeatherModel alloc] init];
        
    });
    return instance;
}

- (void)loadWeatherWitchArea:(NSString *)city success:(SuccessBlock)successBlock error:(ErrorBlock)errorBlock{
    
    NSString *url = @"http://api.map.baidu.com/telematics/v3/weather";
    NSLog(@"%@",city);
    NSDictionary *paramDic = @{@"location":city,
                               @"output":@"json",
                               @"ak":@"17IvzuqmKrK1cGwNL6VQebF9"
                               };
    
    [[SDNetWorkTool shareManager] GET_RequestWithUrlString:url parmas:paramDic successBloock:^(id responseObject) {
        
        NSArray *weatherArr = responseObject[@"results"];
        NSArray *weathers = [SDWeatherModel mj_objectArrayWithKeyValuesArray:weatherArr[0][@"weather_data"]];
        NSArray<SDWeatherModel *> *temp = weathers;
        
        for (NSInteger i = 0; i < weathers.count; i++) {
            temp[i].currentCity = weatherArr[0][@"currentCity"];
        }
        weathers = temp;
        if (successBlock) {
            successBlock(weathers);
        }
        
    } failureBlock:^(NSError *error) {
        if (errorBlock) {
            errorBlock(error);
        }
        
    }];
}

@end
